
class MyTest9 {

    public static int count=0;
    synchronized public static void add() {
        count++;
    }
    public int get() {
        return count;
    }
}
public class Test9 {



    //对静态方法加锁--在外面加锁
    public static void main(String[] args) throws InterruptedException {
        MyTest9 myTest9 = new MyTest9();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                MyTest9.add();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                MyTest9.add();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(myTest9.get());

    }
}
